package com.anzia.demo07;

import java.util.Arrays;
import java.util.Locale;

/**
 *@ClassName String_Demo
 *@Description
 *@Author Anzia.
 *@Date 2021/4/11 15:30
 *@Version 1.0
 **/
public class String_Demo {
    public static void main(String[] args) {
        String name = "hello";
        name = "zhangsan";
        String name2 = "zhangsan";

        //演示字符串的另一种创建方式 new String()
        String str = new String("java");
        String str2 = new String("java");
        //这里比较的是hashCode
        System.out.println(str == str2);
        //这里比较的是对象的值
        System.out.println(str.equals(str2));

        //字符串方法的使用
        //length() 返回字符串的长度
        //charAt(int index) 返回某个位置的字符
        //contains(String str) 判断是否包含某个子字符串
        String content = "Java是世界上最好的编程语言，Java真香";
        System.out.println(content.length());
        System.out.println(content.charAt(0));
        System.out.println(content.charAt(content.length()-1));
        System.out.println(content.contains("Java"));

        //toCharArray() 返回字符串对应的数组
        //indexOf() 返回字符串首次出现的位置
        //lastIndexOf() 返回字符串最后一次出现的位置
        System.out.println(Arrays.toString(content.toCharArray()));
        System.out.println(content.indexOf("Java"));
        System.out.println(content.indexOf("Java",4));
        System.out.println(content.lastIndexOf("Java"));

        //trim()
        //toUpperCase() toLowerCase()
        //endsWith() startsWith()
        String content2 = "       hello world    ";
        System.out.println(content2.trim());
        System.out.println(content2.toUpperCase());
        String filename = "hello.java";
        System.out.println(filename.endsWith("va"));
        System.out.println(filename.startsWith("hello"));

        //replace()
        //split()
        System.out.println(content.replace("Java","HTML"));
        String say = "Java is the best language in    the world, I love Java";
        System.out.println(Arrays.asList(say.split(" ")));
        System.out.println(Arrays.asList(say.split("[ ,]+")));

        //补充两个方法equals、compareTo()，比较大小
        String s1 = "hello";
        String s2 = "HELLO";
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s2.toLowerCase()));
        System.out.println(s1.equalsIgnoreCase(s2));

        String s3 = "xyz";
        String s4 = "abc";
        System.out.println(s3.compareTo(s4));
    }
}
